(function(){
'use strict';

angular.module('controllers.oldOMDisplay.infoDispaly')
	.controller('businessChannelController', businessChannelController);

function businessChannelController($scope, InfoViewService, COREDATA, CoreService,CoreLoading) {
	//日期错误信息
	$scope.errorInfo = COREDATA.ERRORINFO;   //日期错误信息
	//省份数据
	$scope.provinces = COREDATA.PROVINCES;
	//业务线
	$scope.businessList = COREDATA.BUSINESSLINE;
	//帐期数据
	$scope.timeTypes = COREDATA.TIMETYPES;

	$scope.compares = [{
		markType: '1',
		markTypeName: '同比'
	}, {
		markType: '2',
		markTypeName: '环比'
	}];

	$scope.shoumun = 1;
	//查询条件参数
	$scope.translate = {
		provinceCode: [],
		businessCode: {
			'businessLineCode': '01',
			'businessLineName': '天猫'
		},
		markType: {
			markType: '1',
			markTypeName: '同比'
		},
		timeTypeCode: {
			timeTypeCode: '1',
			timeTypeName: '日'
		},
		startTime: new Date(),
		startQuarter: {
			code: '1',
			quarterName: '第一季度'
		},
		endTime: new Date(),
		endQuarter: {
			code: '1',
			quarterName: '第一季度'
		},
		year: {}
	};

	/**
	 * [ 设置账期起始时间的默认值]
	 * 初始化页面值的默认 查询账期为日
	 * @param {[type]} timeTypeCode [description]
	 */
	$scope.$watch('translate.timeTypeCode', function(newValue) {
		var month = new Date().getMonth(); //当前月份
		var day = new Date().getDate(); //当前天
		var year = new Date().getFullYear(); //当前年份
		switch (newValue.timeTypeCode) {
			case '1':
				$scope.translate.startTime = new Date(year, month, day - 31);
				$scope.translate.endTime = new Date(year, month, day - 1);
				break;
			case '3':
				$scope.translate.startTime = new Date(year, month - 5);
				$scope.translate.endTime = new Date(year, month);
				break;
			case '4':
				$scope.translate.year.startTime = '20141';
				$scope.translate.year.endTime = '20152';
				break;
			case '5':
				$scope.translate.year.startTime = Number(new Date().getFullYear() - 5);
				$scope.translate.year.endTime = Number(new Date().getFullYear());
		}
	});

	$scope.searchChannelBusiness = function() {
		var paramsData = {};
		var queryInfo = angular.copy($scope.translate);
		switch (queryInfo.timeTypeCode.timeTypeCode) {
			case '1':
				paramsData.startTime = CoreService.dateFormat(queryInfo.startTime, 'yyyyMMdd');
				paramsData.endTime = CoreService.dateFormat(queryInfo.endTime, 'yyyyMMdd');
				break;
			case '3':
				paramsData.startTime = CoreService.dateFormat(queryInfo.startTime, 'yyyyMM');
				paramsData.endTime = CoreService.dateFormat(queryInfo.endTime, 'yyyyMM');
				break;
			case '4':
				var start = splitTime(queryInfo.year.startTime);
				var end = splitTime(queryInfo.year.endTime);
				paramsData.startTime = start.year;
				paramsData.startQuarter = start.quarter;
				paramsData.endTime = end.year;
				paramsData.endQuarter = end.quarter;
				break;
			case '5':
				paramsData.startTime = CoreService.dateFormat(queryInfo.startTime, 'yyyy');
				paramsData.endTime = CoreService.dateFormat(queryInfo.endTime, 'yyyy');
				break;
		}
		$scope.searchInfo = angular.copy($scope.translate);
		paramsData.provinceCodes = queryInfo.provinceCode.map(function(item){
			return item.provinceCode;
		});
		paramsData.timeType = queryInfo.timeTypeCode.timeTypeCode;
		$scope.shoumun == 1 ? $scope.shoumun = 0 : $scope.shoumun = 1;
		if ($scope.shoumun === 0) {
			CoreLoading.setState(true);
			InfoViewService.queryChannelAnalysis(paramsData, searchChannelBusiness);
		}

	};

	function splitTime(quarterString) {
		var yearQuarter = {};
		var year = quarterString.substring(0, 4);
		var quarter = quarterString.substring(4, 5);
		yearQuarter.year = year;
		yearQuarter.quarter = quarter;
		return yearQuarter;
	}

	function searchChannelBusiness(res) {
		var title = [];
		var time = [];
		var percentages = [];
		CoreLoading.setState(false);
		if (res.state === '10000' && res.data.length > 0) {
			$scope.flagObj = true;
			angular.forEach(res.data, function(item) {
				time.push(item.time);
				percentages.push(item.percentage);
			});

			var businessEchart = echarts.init(document.getElementById('channel'));

			var options = {
				title: {
					text: '业务渠道分析',
					x: 'center'
				},
				grid: {
					x2: '13%'
				},
				tooltip: {
					trigger: 'axis'
				},
				legend: {
					orient: 'vertical',
					x: 'right',
					y: 'center',
					data: [$scope.searchInfo.businessCode.businessLineName]
				},
				dataZoom: {
					show: true,
					realtime: true,
					start: 0,
					end: 100
				},
				// calculable: true,
				xAxis: [{
					type: 'category',
					data: time
				}],
				yAxis: [{
					name: '(百分比)',
					type: 'value',
					min: 80,
					max: 130,
					  axisLabel : {
						  formatter: '{value} %'
					  }
				}],
				series: [{
					name: $scope.searchInfo.businessCode.businessLineName,
					type: 'line',
					data: percentages,
					itemStyle: {
						normal: { // 系列级个性化，横向渐变填充
							borderRadius: 5,
							label: {
								show: true,
								textStyle: {
									fontSize: '10',
									fontFamily: '微软雅黑',
									fontWeight: 'bold'
								}
							}
						}
					}
				}]
			};

			businessEchart.setOption(options);


		} else {
			CoreService.dialog(res.state);
		}
	}

	$scope.searchChannelBusiness();

}

})();